package com.qyhl.module_practice.score.mine.act;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeAcitivityBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class PracticeScoreMyActivityFragment extends BaseFragment implements PracticeScoreMyActivityContract.PracticeScoreMyActivityView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private View view;

    private List<PracticeAcitivityBean> mDataList = new ArrayList<>();

    public List<PracticeAcitivityBean> getmDataList() {
        return mDataList;
    }

    private CommonAdapter commonAdapter;

    private String userName;

    private String volId;

    private PracticeScoreMyActivityPresenter mPresenter;

    private int page = 1;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setVolId(String volId) {
        this.volId = volId;
    }

    private boolean isVolunteer;

    public void setVolunteer(boolean volunteer) {
        isVolunteer = volunteer;
    }

    private String instId;

    public void setInstId(String instId) {
        this.instId = instId;
    }

    public static PracticeScoreMyActivityFragment newInstance(String userName, String volId, boolean isVolunteer, String instId) {
        PracticeScoreMyActivityFragment fragment = new PracticeScoreMyActivityFragment();
        fragment.setUserName(userName);
        fragment.setVolId(volId);
        fragment.setVolunteer(isVolunteer);
        fragment.setInstId(instId);
        return fragment;
    }


    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        initView();
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.practice_fragment_score_my_act, null);
    }

    @Override
    protected void create() {
        mPresenter = new PracticeScoreMyActivityPresenter(this);
        loadData();
    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));

        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeAcitivityBean>(getContext(), R.layout.practice_item_act, mDataList) {
            @SuppressLint("SetTextI18n")
            @Override
            protected void convert(ViewHolder holder, PracticeAcitivityBean bean, int position) {
                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(getContext().getApplicationContext())
                        .load(bean.getLogo())
                        .apply(new RequestOptions().error(R.drawable.cover_large_default).placeholder(R.drawable.cover_large_default))
                        .into(cover);

                TextView title = holder.getView(R.id.title);
                TextView address = holder.getView(R.id.address);
                TextView endTime = holder.getView(R.id.end_time);
                TextView serviceTime = holder.getView(R.id.service_time);
                TextView loveCounts = holder.getView(R.id.love_counts);
                TextView personTxt = holder.getView(R.id.person_txt);
                TextView personNum = holder.getView(R.id.person_num);
                TextView host = holder.getView(R.id.host);
                TextView expectTime = holder.getView(R.id.expect_time);

                ImageView status = holder.getView(R.id.status);

                title.setText(bean.getName());
                address.setText("地址：" + bean.getAddress());
                endTime.setText("招募截止：" + DateUtils.practiceSignFormat(bean.getEndTime()));

                serviceTime.setText("服务时间：" + DateUtils.practiceServiceFormat(StringUtils.isEmpty(bean.getRegistrationStart()) ? "" : bean.getRegistrationStart()) + " -" + DateUtils.practiceServiceFormat(StringUtils.isEmpty(bean.getRegistrationDeadline()) ? "" : bean.getRegistrationDeadline()));
                if(bean.getActSignType()==1){
                    expectTime.setVisibility(View.GONE);
                }else{
                    expectTime.setVisibility(View.VISIBLE);
                    expectTime.setText("预计服务时长：" + bean.getServiceTime() + "小时");
                }

                loveCounts.setText(bean.getScore() + "分");

                host.setText("主办单位：" + bean.getSource());

                holder.setVisible(R.id.person_layout, false);

                if (bean.getSigninType() == 0) {
                    holder.setText(R.id.sign_type, "定位签到");
                } else {
                    holder.setText(R.id.sign_type, "扫码签到");
                }

                switch (bean.getStatus()) {
                    case "RECRUIT":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        address.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        serviceTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));
                        host.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        expectTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_join_icon)
                                .into(status);
                        break;
                    case "SERVICE":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        address.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        serviceTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));
                        host.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        expectTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_service_icon)
                                .into(status);
                        break;
                    case "BEGINNING":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        address.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        serviceTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));
                        host.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        expectTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_on_icon)
                                .into(status);

                        break;
                    case "END":
                        title.setTextColor(getResources().getColor(R.color.practice_global_black_lv1_60per));
                        address.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        serviceTime.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        personNum.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        host.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        expectTime.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_end_icon)
                                .into(status);
                        break;
                    case "NOT_BEGIN":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        address.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        serviceTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));
                        host.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        expectTime.setTextColor(getResources().getColor(R.color.practice_normal_color));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_forecast_icon)
                                .into(status);
                        break;
                    default:
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        address.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        serviceTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));
                        host.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        expectTime.setTextColor(getResources().getColor(R.color.practice_normal_color));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_on_icon)
                                .into(status);
                        break;
                }
            }
        });
        if(isVolunteer){
            mPresenter.getList(volId, page + "");
        }else{
            mPresenter.getPublicList(userName,page+"");
        }

    }

    @Override
    protected void setListener() {
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if(isVolunteer){
                    mPresenter.getList(volId, page + "");
                }else{
                    mPresenter.getPublicList(userName,page+"");
                }
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if(isVolunteer){
                    mPresenter.getList(volId, page + "");
                }else{
                    mPresenter.getPublicList(userName,page+"");
                }
            }
        });

        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                if(isVolunteer){
                    mPresenter.getList(volId, page + "");
                }else{
                    mPresenter.getPublicList(userName,page+"");
                }
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle =  new Bundle();
                bundle.putString("id", mDataList.get(position).getId() + "");
                bundle.putString("userName", userName);
                bundle.putString("title", mDataList.get(position).getName());
                bundle.putString("instId",instId);
                RouterManager.router(ARouterPathConstant.PRACTICE_ACT_DETAIL,bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void setList(List<PracticeAcitivityBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        page++;
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            if (!NetUtil.isNetworkAvalible(getContext())) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }
}
